home *** CD-ROM | disk | FTP | other *** search
/ Dr. Windows 3 / dr win3.zip / dr win3 / PROGRAMR / BOOTSEC.ZIP / BOOTSEC.C < prev    next >
C/C++ Source or Header  |  1993-07-08  |  25KB  |  662 lines

  1. //****************************************************************************
  2. // File:        BOOTSEC.C        
  3. //          
  4. //
  5. // Purpose:     The main module.  Brings up a private dialog, that prompts 
  6. //              the user for a drive letter.  When the user chooses "Get
  7. //              Info", BOOTSEC determines what kind of drive it is, and then if 
  8. //              appropriate, reads the boot sector of the drive, and displays
  9. //              all the information from the bootsector in the dialog.
  10. //
  11. // Functions:
  12. //            WinMain()       -  initializes app and processes message loop 
  13. //            ActualDlgProc         -  the actual Dialog Procedure
  14. //            AboutDlgProc    -  Dialog procedure for the About box
  15. //            ClassDlgProc    -  the Window Proc for the Private Dialog
  16. //            NewEditProc     -  the subclassed Edit window procedure 
  17. //            ReadBootSector  -  read the boot sector via INT 25
  18. //            ShowDriveInfo   -  display the boot sector structure
  19. //            GetPictRect     -  get a rect to display a drive icon
  20. //            IsCDRomDrive    -  is it a CD-ROM drive?
  21. //            IsNetDrive      -  is it a network drive?
  22. //            SetAllLabels    -  show a given string in all the labels 
  23. //            MyGetDriveType  -  return the type of drive
  24. //
  25. // Development Team:                   
  26. //
  27. //                  Joe Long, June 1993
  28. //
  29. //
  30. // Written by Microsoft Product Support Services, Windows Developer Support
  31. // Copyright (c) 1992 Microsoft Corporation. All rights reserved.
  32. //****************************************************************************
  33.  
  34.  
  35. #include "windows.h"                           
  36. #include "bootsec.h"
  37. #include "memory.h"
  38. #include "resource.h"
  39. #define CBSECTORSIZE 512
  40.  
  41. // global vars
  42. int iDriveType;          // what kind of drive is selected
  43. HINSTANCE hInst;         // the instance handle
  44. FARPROC lpfnOldEditProc; // the original edit procedure                     
  45. char szErrorBuf[64];     // a buffer to load error strings
  46.  
  47. // exported function
  48. int PASCAL WinMain(HANDLE hInstance, HANDLE hPrevInstance, LPSTR lpCmdLine, int nCmdShow);
  49. BOOL FAR PASCAL __export ActualDlgProc (HWND hDlg, UINT message,  WPARAM wParam, LPARAM lParam);
  50. BOOL FAR PASCAL __export AboutDlgProc (HWND hDlg, UINT message,  WPARAM wParam, LPARAM lParam);
  51. long FAR PASCAL __export ClassDlgProc(HWND hDlg, UINT message, WPARAM wParam , LPARAM lParam);
  52. long FAR PASCAL __export NewEditProc(HWND hEdit, UINT message, WPARAM wParam , LPARAM lParam);
  53.  
  54. //helper functions
  55. BOOL ReadBootSector(int iDrive, PSTR pBuf);
  56. void ShowDriveInfo(HWND hDlg, BOOTSECTOR *bs);
  57. void GetPictRect(HWND hWnd, LPRECT lpRect);   
  58. BOOL IsCDRomDrive(int iDrive);    
  59. WORD IsNetDrive(int iDrive);            
  60. void SetAllLabels(HWND hDlg, LPSTR szText);              
  61. int MyGetDriveType(BOOTSECTOR *bs);
  62.  
  63.  
  64. /****************************************************************************
  65.  
  66.     FUNCTION: WinMain(HANDLE, HANDLE, LPSTR, int)
  67.  
  68.     PURPOSE: calls initialization function, processes message loop
  69.  
  70. ****************************************************************************/
  71.  
  72. int PASCAL WinMain(HANDLE hInstance, HANDLE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
  73. {
  74.     MSG msg;
  75.     WNDCLASS wc;
  76.     HWND hwnd;  
  77.  
  78.     DLGPROC dlgProc;
  79.     hInst = hInstance;  
  80.  
  81.     if (!hPrevInstance)
  82.     {
  83.         memset(&wc,NULL, sizeof(WNDCLASS));
  84.         wc.lpfnWndProc = ClassDlgProc;   
  85.         wc.cbWndExtra = DLGWINDOWEXTRA;
  86.         wc.hInstance = hInstance;
  87.         wc.hIcon = LoadIcon(hInstance,MAKEINTRESOURCE(IDI_MAIN));
  88.         wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  89.         wc.hbrBackground = COLOR_WINDOW + 1;
  90.         wc.lpszClassName = "BootSectorClass";    
  91.         RegisterClass(&wc);    
  92.     }               
  93.     dlgProc = (DLGPROC)MakeProcInstance(ActualDlgProc, hInst); 
  94.     hwnd = CreateDialog(hInstance, MAKEINTRESOURCE(IDD_MAINDIALOG), 0, dlgProc);        
  95.     ShowWindow(hwnd,nCmdShow);
  96.     while (GetMessage(&msg,NULL,0,0))
  97.     {
  98. // if we want to be able to use the TAB key
  99. // to move between controls, the ENTER
  100. // key to execute the default button, 
  101. // the ESCAPE key to dismiss the dialog, 
  102. // or any other dialog - type functionality,
  103. // we must call IsDialogMessage() 
  104.         if (!IsDialogMessage(hwnd, &msg))
  105.         {
  106.             TranslateMessage(&msg);
  107.             DispatchMessage(&msg);    
  108.         }
  109.     
  110.     }                                                                     
  111.     FreeProcInstance((FARPROC)dlgProc);
  112.     return msg.wParam;                            
  113.     
  114.     
  115. }   
  116.  
  117.  
  118. /****************************************************************************
  119.  
  120.     ClassDlgProc(HWND hDlg, UINT message, WPARAM wParam , LPARAM lParam)
  121.  
  122.     PURPOSE: 
  123.      this function gets placed between the dialog and the DefDlgProc because
  124.       
  125.         1. its a private dialog 
  126.      and
  127.         2. we specified a DLGPROC for the  third parameter 
  128.            of the CreateDialog() call.
  129.            
  130.     we could handle all of the messages here (except for the WM_INITDIALOG 
  131.     message which is not sent to non-dialogs, or we can pass the messages 
  132.     off to DefDlgProc(), which will then call our dialog procedure 
  133.     ActualDlgProc(), given below    
  134.  
  135. ****************************************************************************/
  136. long FAR PASCAL __export ClassDlgProc(HWND hDlg, UINT message, WPARAM wParam , LPARAM lParam)
  137. {
  138.   
  139.     return DefDlgProc(hDlg, message, wParam, lParam);
  140.     
  141. }      
  142.  
  143.  
  144. /****************************************************************************
  145.  
  146.     ActualDlgProc (HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
  147.  
  148.     PURPOSE: 
  149.     The procedure for the application that does most of the work.
  150.     
  151.     This is the function that we passed in as the last parameter to the
  152.     CreateDialog() call.  We do this so that we can get the WM_INITDIALOG
  153.     message, which is not passed to the WndProc of a Private Dialog.
  154.     
  155.     We subclass the edit control so that we can restrict input to capital
  156.     letters, the backspace key, and the TAB key. 
  157.  
  158. ****************************************************************************/
  159.        
  160. BOOL FAR PASCAL __export ActualDlgProc (HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
  161. {
  162.     static HDC hMemDC;
  163.     static HBITMAP hBitmap, hbmOld;
  164.   
  165.     switch (message)
  166.     {        
  167.         case WM_INITDIALOG:
  168.         {
  169.             HDC hdc;            
  170.             HWND hwndEdit; 
  171.             HMENU hSysMenu;
  172.             hSysMenu = GetSystemMenu(hDlg, FALSE);                            
  173.             // disable the "maximize" option in the system menu
  174.             EnableMenuItem(hSysMenu, 4, MF_GRAYED|MF_DISABLED|MF_BYPOSITION); 
  175.             // disable the "size" option of the system menu                   
  176.             EnableMenuItem(hSysMenu, 2, MF_GRAYED|MF_DISABLED|MF_BYPOSITION); 
  177.                    
  178.             // subclass the edit control so that we
  179.             // can restrict input to letter only
  180.             hwndEdit = GetDlgItem(hDlg, IDC_DRIVE);
  181.             if (hwndEdit)
  182.                 lpfnOldEditProc = (FARPROC)SetWindowLong(hwndEdit, GWL_WNDPROC, (LONG)NewEditProc);    
  183.                 
  184.             // limit the text of the edit control to 1 character
  185.             SendMessage(hwndEdit, EM_LIMITTEXT, 1, 0L);
  186.             
  187.             // put a reasonable default into the edit control
  188.             SetWindowText(hwndEdit, "C");
  189.             
  190.  
  191.             hBitmap = LoadBitmap(hInst, MAKEINTRESOURCE(IDB_DRIVES));
  192.             hdc = GetDC(NULL);
  193.             hMemDC = CreateCompatibleDC(hdc);
  194.             ReleaseDC(NULL, hdc);
  195.             hbmOld = SelectObject(hMemDC, hBitmap);        
  196.             iDriveType = BM_NONE;
  197.             return FALSE;   // didn't set the focus
  198.             
  199.         }
  200.         break;                                     
  201.         case WM_PAINT:  
  202.         {
  203.             PAINTSTRUCT ps;
  204.             RECT rect;            
  205.             BeginPaint(hDlg, &ps);
  206.             GetPictRect(hDlg